<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>检修任务列表</title>
    <script type="text/javascript">
      var newTrainTaskWin;
      var editTrainTaskWin;
      var recordListWin;
      var readMaintenanceWin;
      var trainTaskDS;
      var trainTaskSn;                                                          //全局变量，用作存放选择检修任务的sn，给后面的窗口显示标题

      function readMaintenanceWindow(sub_type,trainTaskId,trainId,taskBeginMinute,isEnd,sn,trainSn){
        readMaintenanceWin = new Ext.Window({                                   //window和上面的fuction不能重名，否则会出问题
          layout: 'fit',
          width:1019,
          height:590,
          closeAction: 'destroy',
          autoLoad: {
            url: "/maintenance/maintenanceList",
            params:{
              trainTaskId:trainTaskId,
              taskBeginMinute:taskBeginMinute,
              sn:sn,
              sub_type:sub_type,
              isEnd:isEnd,
              trainId:trainId
            },
            scripts: true
          },
          plain: true,
          modal: true,
          constrain: true,
          autoScroll: true
        });
        readMaintenanceWin.on("destroy",function(){
          trainTaskDS.reload();
        });                                                   //将sn复制给全局变量trainTaskSn
        readMaintenanceWin.setTitle(trainSn+"的维护单元管理");
        readMaintenanceWin.resizable = false;
        readMaintenanceWin.show();
      }

      function readChartWindow(sub_type,isEnd,trainTaskId,trainId,taskBeginMinute,sn,date){
        readChartWin = new Ext.Window({                                         //window和上面的fuction不能重名，否则会出问题
          layout: 'fit',
          width:939,
          height:642,
          closeAction: 'destroy',
          autoLoad: {
            url: "/maintenance/chart",
            params:{
              trainTaskId:trainTaskId,
              taskBeginMinute:taskBeginMinute,
              sub_type:sub_type,
              isEnd:isEnd,
              trainId:trainId
            },
            scripts: true
          },
          plain: true,
          modal: true,
          constrain:true,
          autoScroll: true
          
        });
        readChartWin.on("destroy",function(){
          clearInterval(interval);
          trainTaskDS.reload();
        });
        trainTaskSn = sn;                                                       //将sn复制给全局变量trainTaskSn
        readChartWin.setTitle(trainTaskSn+"的工作进度表--"+date);
        readChartWin.resizable = false;
        readChartWin.show();
      }

      Ext.onReady(function(){
        //检修任务管理---------------------------------------------------------
        //生成有关检修任务的Grid

        var trainDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/train/getTrainsForCombo'}),
          reader:new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'train'
          }, [
            {name:'sn'},                           
            {name:'id'}                               
          ])
        });
        var trainIdCombo = new Ext.form.ComboBox({
          store: trainDS,
          hiddenName:'train_id',           
          displayField:'sn',            
          valueField:'id',                
          typeAhead: true,
          disable:true,
          minChars:2,
          readOnly:false,
          width:100,
          mode: 'remote',
          fieldLabel:'列车选择',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择列车.....',
          selectOnFocus:true,
          anchor: '96%'
        });

        var beginTimeSearch = new Ext.ux.form.DateTimeField({
          fieldLabel: '开始时间范围',
          name: 'begin_time_search',
          id: 'begin_time_search',
          format: 'Y-m-d', //格式化日期
          invalidText: "您输入的日期格式不对，格式应如：2010-01-01",
          anchor: '96%'
        });

        var endTimeSearch = new Ext.ux.form.DateTimeField({
          fieldLabel: '结束时间范围',
          name: 'end_time_search',
          id: 'end_time_search',
          format: 'Y-m-d', //格式化日期
          invalidText: "您输入的日期格式不对，格式应如：2010-01-01",
          anchor: '96%'
        });

        var searchButton = new Ext.Button({
          text:'搜索',
          fieldLabel:'点击搜索',
          iconCls: 'searchItem',
          handler: function(){
            trainTaskDS.baseParams.begin_time = beginTimeSearch.getValue();
            trainTaskDS.baseParams.end_time = endTimeSearch.getValue();
            trainTaskDS.baseParams.train_id = trainIdCombo.value;
            trainTaskDS.load({
              params:{
                start : 0,
                limit : pageSize
              }
            })
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            searchTrainTaskForm.form.reset();
          }
        });

        trainTaskDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/traintask/getTrainTasks'
          }),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'root'
          },[
            {name:'id'},
            {name:'sn'},
            {
              name:'sub_type_char',
              mapping:'sub_type',
              convert:function(value){
                if(value==0){
                  return '静态作业'
                }else if(value==1){
                  return '动态作业'
                }else{
                  return value
                }
              }
            },
            {name:'begin_time'},
            {
              name:'is_end',
              mapping:'is_end',
              convert:function(value){
                if(value==0){
                  return '<font color=red><center>电力调整中</center></font>'
                }else if(value==1){
                  return '<font color=black><center>已完成</center></font>'
                }else if(value==2){
                  return '<font color=green><center>静态作业中</center></font>'
                }else if(value==3){
                  return '<font color=black><center>动态作业中</center></font>'
                }else{
                  return '<font color=yellow><center>状态异常</center></font>'
                }
              }
            },
            {name: 'end_time'},
            {
              name:'isEnd',
              mapping:'is_end'
            },
            {name:'created_at'},
            {name:'updated_at'},
            {name:'stop_track'},
            {name:'device_id'},
            {name:'train_sn',
              mapping: 'train',
              convert:function(value){
                if(value){
                  return value.sn
                }else{
                  return '未设置'
                }
              }
            },
            {name:'train_id',
              mapping: 'train',
              convert:function(value){
                if(value){
                  return value.id
                }else{
                  return '未设置'
                }
              }
            }
          ])
        });
        var trainTaskSM = new Ext.grid.CheckboxSelectionModel();
        var trainTaskCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          trainTaskSM,
          {header:'DB',sortable:true,dataIndex: 'id',width:50,hidden:true},
          {header:'<center>检修开始时间</center>',sortable:true,dataIndex: 'begin_time',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width:140},
          {header:'<center>检修结束时间</center>',sortable:true,dataIndex: 'end_time',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width:140},
          {header:'<center>管理维护单元</center>',dataIndex: 'manufacturer',renderer: maintenanceRender,width:110},
          {header:'<center>列车编号</center>',sortable:true,dataIndex:'train_sn',width:130},
          {header:'<center>停放股道</center>',sortable:true,dataIndex:'stop_track',width:100},
          {header:'<center>进度</center>',dataIndex: 'is_end',width:90},
          {header:'<center>查看维护图表</center>',dataIndex: 'manufacturer',renderer: chartRender,width:100},
          {header:'<center>建立任务时间</center>',dataIndex: 'created_at',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width:130}
        ]);

        function maintenanceRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var trainTaskId = store.getAt(rowIndex).get('id');
          var trainId = store.getAt(rowIndex).get('train_id');
          var taskBeginTime = store.getAt(rowIndex).get('begin_time');
          var isEnd = store.getAt(rowIndex).get('isEnd');
          var sub_type = store.getAt(rowIndex).get('sub_type');
          var sn = store.getAt(rowIndex).get('sn').toString();
          var trainSn = store.getAt(rowIndex).get('train_sn').toString();
          var day = 2                                                           //因为不能传送时间，因此在此对时间进行转换
          var hour = taskBeginTime.slice(11,13)
          var minute = taskBeginTime.slice(14,16)
          var taskBeginMinute= day*1440 + hour*60 + minute*1
          while (taskBeginMinute> 1920)
          {
            taskBeginMinute = taskBeginMinute-1440
          }
      
          return "<a style=cursor:pointer;color:blue onclick=readMaintenanceWindow("
            +sub_type+","+trainTaskId+","+trainId+","+taskBeginMinute+","+isEnd+",'"+sn+"','"+trainSn+"')><center>进入</center></a>"
        }

        function chartRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var trainTaskId = store.getAt(rowIndex).get('id');
          var trainId = store.getAt(rowIndex).get('train_id');
          var taskBeginTime = store.getAt(rowIndex).get('begin_time');
          var isEnd = store.getAt(rowIndex).get('isEnd');
          var sub_type = store.getAt(rowIndex).get('sub_type');
          train_sn = store.getAt(rowIndex).get('train_sn').toString();
          var day = 2                                                           //因为不能传送时间，因此在此对时间进行转换
          var date = taskBeginTime.slice(0,10)
          var hour = taskBeginTime.slice(11,13)
          var minute = taskBeginTime.slice(14,16)
          var taskBeginMinute = day*1440 + hour*60 + minute*1
          while (taskBeginMinute> 1920)
          {
            taskBeginMinute = taskBeginMinute-1440
          }
          return "<a style=cursor:pointer;color:green onclick=readChartWindow("
            +sub_type+","+isEnd+","+trainTaskId+","+trainId+","+taskBeginMinute+",'"+train_sn+"','"+date+"')><center>进入</center></a>"
        }

        var searchTrainTaskForm = new Ext.FormPanel({
          labelWidth: 105, // label settings here cascade unless overridden
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'right',
          bodyStyle:'padding:5px 5px 0',
          width: screenWidth-200,
          items: [
            {
              xtype: 'fieldset',
              defaults: {width: 750},
              title: '条件搜索',
              border: true,
              items: [
                {
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .4,
                      layout: 'form',
                      border: false,
                      items: [beginTimeSearch]
                    },{
                      columnWidth: .4,
                      layout: 'form',
                      border: false,
                      items: [endTimeSearch]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .4,
                      layout: 'form',
                      border: false,
                      items: [trainIdCombo]
                    },{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    },{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });

        var trainTaskGrid =  new Ext.grid.GridPanel({
          ds: trainTaskDS,
          cm: trainTaskCM,
          sm: trainTaskSM,
          width: screenWidth-200,
          height: screenHeight-350,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: trainTaskDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '新增检修任务',
              width:50,
              iconCls: 'addItem',
              handler : function(){

                newTrainTaskWin = new Ext.Window({
                  layout: 'fit',
                  width:797,
                  height:548,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  title: '新增检修任务',
                  autoLoad: {
                    url: "/traintask/newTrainTask",
                    scripts: true
                  }
                });
                newTrainTaskWin.on("destroy",function(){
                  trainTaskDS.reload();
                });
                newTrainTaskWin.resizable = false;
                newTrainTaskWin.show();
              }

            },{
              text: '编辑检修任务',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = trainTaskGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  var trainTaskId = records[0].get('id');
                  editTrainTaskWin = new Ext.Window({
                    title: '编辑检修任务',
                    layout:'fit',
                    width:797,
                    height:548,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/traintask/editTrainTask",
                      params: {
                        trainTaskId: trainTaskId
                      },
                      scripts: true
                    }
                  });
                  editTrainTaskWin.on("destroy",function(){
                    trainTaskDS.reload();
                  });
                  editTrainTaskWin.resizable = false;
                  editTrainTaskWin.show();
                }
              }
            },
            {
              text:'删除检修任务',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(trainTaskGrid.getSelectionModel().hasSelection()){
                  var rs=trainTaskGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var trainTaskSn='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    trainTaskSn+=data.sn+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下检修任务？<br><font color="red">' + trainTaskSn + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/traintask/delete',
                        method:'post',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            trainTaskDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除检修任务。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的检修任务');
                }
              }
            },{
              text: '查看打卡记录',
              width:50,
              iconCls: 'disk',
              handler : function(){
                var records = trainTaskGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再查看。');
                }else{
                  var trainTaskId = records[0].get('id');
                  recordListWin = new Ext.Window({
                    title: '打卡记录',
                    layout:'fit',
                    width:867,
                    height:548,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/checkrecord/checkrecordList",
                      params: {
                        trainTaskId: trainTaskId
                      },
                      scripts: true
                    }
                  });
                  recordListWin.resizable = false;
                  recordListWin.show();
                }
              }
            }
          ]
        });

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-180,
          frame:true,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[searchTrainTaskForm,trainTaskGrid]
        });

        trainTaskDS.load();
        workbenchPanel.render('trainTaskList');
      })
    </script>
  </head>
  <body>
    <div id="trainTaskList"></div>
  </body>
</html>
